import util
import uix
import uiutil
import xtriui
import blue
import uthread
import uiconst
import uicls

def CheckSameClass(a, b):
    if not (hasattr(a, '__guid__') and hasattr(b, '__guid__')):
        return 0
    if (a.__guid__ == b.__guid__):
        return 1



class Checkbox(uicls.CheckboxCore):
    __guid__ = 'uicls.Checkbox'
    default_pos = (0, 0, 100, 0)
    default_prefstype = ('user', 'ui')

    def ApplyAttributes(self, attributes):
        self.pos = attributes.get('pos', self.default_pos)
        self.prefstype = attributes.get('prefstype', self.default_prefstype)
        if (self.prefstype == 1):
            self.prefstype = ('char', 'ui')
        elif (self.prefstype == 2):
            self.prefstype = 'prefs'
        uicls.CheckboxCore.ApplyAttributes(self, attributes)



    def __getattr__(self, k):
        if ((k in ('sr', 'wr')) and ('sr' not in self.__dict__)):
            self.sr = uiutil.Bunch()
            self.wr = util.WeakRefAttrObject()
        if ((k == 'groupName') and self.__dict__.has_key('_groupName')):
            return self.__dict__['_groupName']
        if ((k == 'checked') and self.__dict__.has_key('_checked')):
            return self.__dict__['_checked']
        if (k in self.__dict__):
            return self.__dict__[k]
        raise AttributeError, k



    def _Initialize(self, *args, **kw):
        uiutil.Merge(blue.os.LoadObject('res:/UI/Component/checkbox_new.blue'), self)
        uicls.CheckboxCore._Initialize(self, *args, **kw)



    def Prepare_(self):
        if (self.GetAlign() == uiconst.TOPRIGHT):
            left = 0
            width = 16
        else:
            left = 16
            width = 0
        self.sr.label = uicls.Label(text='', parent=self, name='text', align=uiconst.TOALL, fontsize=10, letterspace=1, state=uiconst.UI_DISABLED, pos=(left,
         3,
         width,
         0), uppercase=1, autowidth=False, autoheight=False)



    def Prepare_Diode_(self):
        self.sr.diode = uiutil.GetChild(self, 'diode')
        uix.Flush(self.sr.diode)
        diode = blue.os.LoadObject(['res:/UI/Component/diode_2.blue', 'res:/UI/Component/square_bumped_2.blue'][(self._groupName is None)])
        if (self._groupName is None):
            diode.left = diode.top = -2
        else:
            diode.left = diode.top = 0
        hdiff = max(0, (self.sr.diode.height - 8))
        self.sr.diode.width = self.sr.diode.height = 8
        self.sr.diode.top = ((((hdiff / 2) + 1) + 2) + 2)
        diode.align = uix.UI_ALCLIENT
        self.selection = uiutil.GetChild(diode, 'selection')
        self.sr.diode.children.append(diode)
        if (self.GetAlign() == uiconst.TOPRIGHT):
            self.sr.diode.anchors = uiconst.UI_ANCHRIGHT



    def SetChecked(self, onoff, report = 1):
        if (onoff is None):
            onoff = 0
        self._checked = int(onoff)
        if (self.sr.diode is None):
            self.Prepare_Diode_()
        self.selection.state = [uix.UI_HIDDEN, uix.UI_DISABLED][self._checked]
        check = uiutil.FindChild(self, 'self_ok')
        if check:
            check.state = self.selection.state
        if report:
            self.UpdateSettings()
            if self.OnChange:
                self.OnChange(self)



    def InitActiveFrame(self, *args):
        if getattr(self, 'activeFrameInited', 0):
            return 
        if self.groupName:
            icon = uicls.Sprite(parent=self.sr.diode, left=-4, top=-4, width=16, height=16)
            icon.texture.pixels = 'res:/UI/Texture/UITexLib_03.dds'
            icon.rectLeft = 55
            icon.rectTop = 141
            icon.rectWidth = icon.rectHeight = 16
            icon.color.a = 0.75
            self.sr.active = icon
            self.sr.active.state = uiconst.UI_HIDDEN
        else:
            self.sr.active = uicls.Frame(parent=self.sr.diode, left=-2, top=-2, width=-2, height=-2, color=(1.0, 1.0, 1.0, 0.75))
            self.sr.active.state = uiconst.UI_HIDDEN
        self.activeFrameInited = 1



    def OnSetFocus(self, *args):
        if (self and ((not self.destroyed) and (self.parent and (self.parent.name == 'inlines')))):
            if (self.parent.parent and self.parent.parent.sr.node):
                browser = uiutil.GetBrowser(self)
                if browser:
                    uthread.new(browser.ShowObject, self)
        self.InitActiveFrame()
        if self.sr.active:
            self.sr.active.state = uix.UI_DISABLED



    def OnKillFocus(self, *etc):
        self.InitActiveFrame()
        if self.sr.active:
            self.sr.active.state = uix.UI_HIDDEN



    def _OnClick(self, *args):
        if ((not self) or self.destroyed):
            return 
        if self._checked:
            uicore.Message('DiodeDeselect')
        else:
            uicore.Message('DiodeClick')
        uicls.CheckboxCore._OnClick(self, *args)



    def RefreshHeight(self):
        label = uiutil.GetChild(self, 'text')
        minHeight = 12
        if self.sr.diode:
            minHeight = 18
        self.height = max(minHeight, ((label.textheight + label.top) + 1))



    def SetLabel(self, labeltext):
        self.SetLabelText(labeltext)



    def ChangeWidth(self, *args):
        if (self.GetAlign() == uiconst.TOPRIGHT):
            offset = self.sr.label.width
        else:
            offset = self.sr.label.left
        self.width = max(20, (offset + self.sr.label.textwidth))




